//
//  SpeedPurViewModel.m
//  GLGW_Shoping
//
//  Created by yang on 2018/11/12.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "SpeedPurViewModel.h"
#import "AdressSelectTableViewCell.h"
#import "PopupView.h"
#import "InputPurchaseDayView.h"
#import "DepositPurTableViewCell.h"


@interface SpeedPurViewModel ()<UITableViewDelegate,UITableViewDataSource,ProdectNameDelegate>{
    PurchaseType v_PType;
}
@end

@implementation SpeedPurViewModel

-(instancetype)initWithViewToViewModel:(UITableView *)tableView thePurchaseType:(PurchaseType)pur_type theSuperView:(UIView *)superView{
    if (self = [super initWithViewToViewModel:tableView theSuperView:superView]) {
        v_PType = pur_type;
        self.purInforModel = [PurchareModel new];
        self.purInforModel.isSettlementTogether = 2;
        tableView.height = tableView.height - kHeightProportion_In_iPhoneTriploid(44);
        tableView.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.mj_footer.hidden = YES;
        tableView.mj_header.hidden = YES;
        tableView.tableHeaderView = [self createHeaderView];
        [superView addSubview:tableView];
        //
        GLGWButton * submitBut = [[GLGWButton alloc] initWithFrame:CGRectMake(0, kY_With_View(tableView, 0), KScreenWidth, kHeightProportion_In_iPhoneTriploid(44)) titleText:@"提交求购单" titleLableFont:SYSTEMFONT(16) titleLableColor:[UIColor whiteColor] inSuperView:superView];
        submitBut.backgroundColor = kColor_Rule_Yellow;
        [submitBut addTarget:self action:@selector(comitAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

-(UIView *)createHeaderView{
    self.headerView = [[SpeedHeaderView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(423))];
    self.headerView.delegate = self;
    return self.headerView;
}

-(void)comitAction{//发布
    if (self.Block_SubmitAction) {
        self.Block_SubmitAction();
    }
}
#pragma mark -- set

#pragma mark -- 数据请求
-(void)requestPublishData{
    NSDictionary * publishDic = @{@"":@""};
    [GLGWRequestTool POST:[NSString stringWithFormat:@"%@%@",kApiPrefix,kPublishPurchases] lodingView:nil lodingTextString:@"" isLoding:YES parameters:publishDic success:^(id  _Nullable response) {
        
    } failure:^(NSError * _Nullable error) {
        
    }];
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (v_PType == PurchaseType_Deposit) {
        return 4;
    }else{
        return 3;
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSInteger secRowNum;
    switch (section) {
        case 0:{
            secRowNum = 2;
        }
            break;
        case 1:{
            secRowNum = 3;
        }
            break;
        case 2:{
            secRowNum = 4;
        }
            break;
            
        default:
            secRowNum = 1;
            break;
    }
    return secRowNum;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 3) {
        return kHeightProportion_In_iPhoneTriploid(66);;
    }else{
        return kHeightProportion_In_iPhoneTriploid(44);
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BaseTableViewCell * cell;
    if (indexPath.section == 3) {
        DepositPurTableViewCell * cell_deposit = [DepositPurTableViewCell cellWithTableView:tableView];
        cell = cell_deposit;
    }else{
        AdressSelectTableViewCell * cell_list = [AdressSelectTableViewCell cellWithTableView:tableView];
        NSString * leftTitStr;
        switch (indexPath.section) {
            case 0:{
                if (indexPath.row == 0) {
                    leftTitStr = @"选择收货地址";
                    cell_list.inputTextField.placeholder = @"";
                }else{
                    leftTitStr = @"报价截止时间";
                }
                cell_list.showPush = YES;
            }
                break;
            case 1:{
                if (indexPath.row == 0) {
                    leftTitStr = @"一票结算";
                    cell_list.inputTextField.placeholder = @"";
                    cell_list.showSwitch = YES;
                    cell_list.switchSelect = self.purInforModel.isSettlementTogether==2?NO:YES;
                    cell_list.showPush = NO;
                    cell_list.Block_SwitchACtion = ^(BOOL s_Bool) {
                        if (s_Bool) {
                            self.purInforModel.isSettlementTogether = 1;
                        }else{
                            self.purInforModel.isSettlementTogether = 2;
                        }
                    };
                }else if (indexPath.row == 1){
                    leftTitStr = @"支付方式";
                    cell_list.showPush = YES;
                }else{
                    leftTitStr = @"物流要求";
                    cell_list.showPush = YES;
                }
            }
                break;
            case 2:{
                if (indexPath.row == 0) {
                    leftTitStr = @"买家公司名称";
                    cell_list.inputTextField.textAlignment = NSTextAlignmentLeft;
                    cell_list.inputTextField.placeholder = @"请填写买家公司名称";
                    cell_list.inputTextField.userInteractionEnabled = YES;
                    [cell_list.inputTextField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
                        self.purInforModel.companyName = x;
                    }];
                    cell_list.showPush = NO;
                }else if (indexPath.row == 1){
                    leftTitStr = @"联系人";
                    cell_list.inputTextField.textAlignment = NSTextAlignmentLeft;
                    cell_list.inputTextField.placeholder = @"请填写联系人姓名";
                    cell_list.inputTextField.userInteractionEnabled = YES;
                    [cell_list.inputTextField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
                        self.purInforModel.contacts = x;
                    }];
                    cell_list.showPush = NO;
                }else if (indexPath.row == 2){
                    leftTitStr = @"联系人手机号";
                    cell_list.inputTextField.textAlignment = NSTextAlignmentLeft;
                    cell_list.inputTextField.placeholder = @"请填写联系人手机号";
                    cell_list.inputTextField.userInteractionEnabled = YES;
                    cell_list.inputTextField.keyboardType = UIKeyboardTypeNumberPad;
                    [cell_list.inputTextField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
                        self.purInforModel.contactPhone = x;
                    }];
                    cell_list.showPush = NO;
                }else{
                    leftTitStr = @"隐私设置";
                    cell_list.showPush = YES;
                    if (v_PType == PurchaseType_Deposit) {
                        cell_list.inputTextField.placeholder = @"报价人支付定金后可见";
                    }
                }
            }
                break;
                
            default:
                break;
        }
        cell_list.leftTitString = leftTitStr;
        cell = cell_list;
    }
    [cell setSepartionLineWith:lineModel_Half];
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(10))];
    return headerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return kHeightProportion_In_iPhoneTriploid(10);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    PopupView * popView;
    NSArray * arr;
    AdressSelectTableViewCell * cell_ad = [tableView cellForRowAtIndexPath:indexPath];
    cell_ad.arr = @[@"30分钟",@"1小时",@"闭市（每日17：00）"];
    switch (indexPath.section) {
        case 0:{
            if (indexPath.row == 1) {
                if (v_PType == PurchaseType_Ordinary || v_PType == PurchaseType_Deposit) {
                    arr = @[@"30分钟",@"1小时",@"闭市（每日17：00）"];
                    popView = [[PopupView alloc] initPopupview:arr];
                    popView.Block_SelectType = ^(NSInteger row) {
                        self.purInforModel.quoteLimit = row+1;
                        cell_ad.inputTextField.text = arr[row];
                    };
                }else{//长期求购
                    arr = @[@"1天",@"2天",@"3天",@"自定义"];
                    popView = [[PopupView alloc] initPopupview:@[@"1天",@"2天",@"3天",@"自定义"]];
                    popView.Block_SelectType = ^(NSInteger row) {
                        if (row == 3) {
                            InputPurchaseDayView * dayInputView = [[InputPurchaseDayView alloc] initWithPopWindowView];
                            [dayInputView show];
                            dayInputView.Block_InpuetDay = ^(NSInteger day) {
                                self.purInforModel.quoteLimit = day;
                                cell_ad.inputTextField.text = [NSString stringWithFormat:@"%ld天",day];
                            };
                        }else{
                            self.purInforModel.quoteLimit = row+1;
                            
                        }
                    };
                    
                }
                
            }
        }
            break;
        case 1:{
            if (indexPath.row == 1) {
                arr = @[@"公对公支付",@"担保支付"];
                popView = [[PopupView alloc] initPopupview:arr];
                popView.Block_SelectType = ^(NSInteger row) {
                    self.purInforModel.payType = row+1;
                    cell_ad.inputTextField.text = arr[row];
                };
            }else if (indexPath.row == 2){
                arr = @[@"买家自提",@"卖家承担"];
                popView = [[PopupView alloc] initPopupview:arr];
                popView.Block_SelectType = ^(NSInteger row) {
                    self.purInforModel.logisticsRequire = row +1;
                    cell_ad.inputTextField.text = arr[row];
                };
            }
        }
            break;
        case 2:{
            if (indexPath.row == 3) {
                arr = @[@"全部可见",@"报价后可见",@"中标后可见",@"始终不公开"];
                popView = [[PopupView alloc] initPopupview:arr];
                popView.Block_SelectType = ^(NSInteger row) {
                    self.purInforModel.privacySetting = row +1;
                    cell_ad.inputTextField.text = arr[row];
                };
            }
        }
            break;
            
        default:
            break;
    }
}
#pragma mark -- ProdectNameDelegate
- (void)cardNumRowModel:(CardInforModel *)car_model{
    [_subject sendNext:car_model];
}
@end
